package com.cloudm.technician.ui.base

import android.content.pm.ActivityInfo
import android.os.Bundle
import android.support.v4.app.FragmentManager
import android.support.v4.content.ContextCompat
import android.support.v7.app.ActionBar
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.Menu
import android.view.View
import android.widget.TextView
import com.cloudm.technician.R
import com.cloudm.technician.extensions.hideSoftKeyboard
import com.cloudm.technician.ui.widget.OnceClickListener
import com.crashlytics.android.Crashlytics
import com.jaeger.library.StatusBarUtil
import com.umeng.analytics.MobclickAgent
import io.fabric.sdk.android.Fabric

/**
 * 作者: wesley
 * 时间: 2017/11/20
 * 描述: 基类的activity
 */
abstract class BaseActivity : AppCompatActivity() {

    private var toolbar: Toolbar? = null
    private var tvTitle: TextView? = null

    protected var headerTitle: Int = R.string.app_name
        set(value) {
            initToolbar()
            tvTitle?.text = resources.getString(value)
        }


    protected var headerTitleString: String? = ""
        set(value) {
            initToolbar()
            tvTitle?.text = value
        }

    protected var displayHomeAsUpEnabled: Boolean = true
        set(value) {
            toolbar?.let {
                val actionBar: ActionBar? = supportActionBar
                actionBar?.setDisplayHomeAsUpEnabled(value)
                if (value) {
                    it.setNavigationOnClickListener(object : OnceClickListener() {
                        override fun onOnceClick(view: View) {
                            onNavigationOnClick()
                        }
                    })
                }
            }
        }


    protected var customerNavigationIcon: Int = R.mipmap.ic_home_user
        set(value) {
            toolbar?.let {
                it.setNavigationIcon(value)
                it.setNavigationOnClickListener(object : OnceClickListener() {
                    override fun onOnceClick(view: View) {
                        onIndicatorClick()
                    }
                })
            }
        }


    protected open val optionsMenuEnabled: Boolean by lazy {
        false
    }

    protected open val menuRes: Int by lazy {
        R.menu.common_menu
    }

    protected open val immEnabled: Boolean by lazy {
        true
    }

    protected open val layoutResID: Int by lazy {
        R.layout.common
    }
    protected val manager: FragmentManager by lazy {
        supportFragmentManager
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(layoutResID)
        initHeaderView()
        handleArgument()
        initVariables()
        initViews(savedInstanceState)
        initListener()
    }

    override fun setContentView(layoutResID: Int) {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        Fabric.with(this, Crashlytics())
        super.setContentView(layoutResID)
        if (immEnabled) {
            StatusBarUtil.setColor(this, ContextCompat.getColor(this, R.color.colorPrimary), 0)
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        if (optionsMenuEnabled) {
            menuInflater.inflate(menuRes, menu)
        }
        return optionsMenuEnabled
    }

    override fun onResume() {
        super.onResume()
        MobclickAgent.onResume(this)
    }

    override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)
        hideSoftKeyboard()
    }


    private fun handleArgument() {
        intent?.let {
            it.extras?.let {
                handleBundle(it)
            }
        }
    }

    private fun initHeaderView() {
        toolbar = findViewById(R.id.toolbar)
        tvTitle = findViewById(R.id.tvHeaderTitle)
    }

    private fun initToolbar() {
        toolbar?.let {
            it.title = ""
            setSupportActionBar(it)
        }
    }


    protected open fun onNavigationOnClick() {
        onBackPressed()
    }


    protected open fun onIndicatorClick() {

    }


    /**
     * 处理界面跳转时参数传递
     */
    protected abstract fun handleBundle(bundle: Bundle)


    /**
     * 初始化变量---adapter
     */
    protected abstract fun initVariables()


    /**
     * 初始化view
     */
    protected abstract fun initViews(savedInstanceState: Bundle?)


    /**
     * 初始化点击事件
     */
    protected abstract fun initListener()

}